package com.luomu.base.biz.cookBook.excel.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.converters.bigdecimal.BigDecimalStringConverter;
import com.alibaba.excel.converters.date.DateStringConverter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSONObject;
import com.luomu.base.api.bo.ReqInfo;
import com.luomu.base.api.bo.RspPage;
import com.luomu.base.api.cookBook.excel.bo.CommonExportResultBO;
import com.luomu.base.api.cookBook.simple.bo.FinanceArApAndInvoiceInfoBO;
import com.luomu.base.api.cookBook.excel.bo.FinanceArApAndInvoiceInfoExcelBO;
import com.luomu.base.api.cookBook.simple.bo.FinanceArApAndInvoiceInfoReqBO;
import com.luomu.base.api.cookBook.simple.service.FinanceArApAndInvoiceInfoService;
import com.luomu.base.biz.cookBook.excel.po.ExportFilePO;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.luomu.base.api.cookBook.excel.contant.ExportResultStatus;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yanhan
 * @since 2024-11-29
 * @version 1.0
 * @Description 二级集采导出接口。调用   handler()
 */

@Service
public class FinanceArApAndInvoiceInfoExcelExportImpl extends AbstractCommonExcelExportStrategy {

	protected Logger logger = LoggerFactory.getLogger(FinanceArApAndInvoiceInfoExcelExportImpl.class);

	@Autowired
	private FinanceArApAndInvoiceInfoService financeArApAndInvoiceInfoService;

//	@Autowired
//	private ExcelExportHelper excelExportHelper;

	/**
	 * 获取来源
	 */
	@Override
	public Integer getOriginType(){
		return 2;
	}

	@Override
	public String getFileName(String requestParam) {

		Date date = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		int number = (int) (Math.random() * 90 + 10);
		return "应收挂帐单_" + simpleDateFormat.format(date)+"_"+number;
	}

	@Override
	public CommonExportResultBO handler(ReqInfo reqInfo, ExportFilePO po) {
		ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
		ExcelWriter excelWriter = EasyExcel.write(out).registerConverter(new BigDecimalStringConverter())
				.registerConverter(new DateStringConverter()).build();
		WriteSheet orderWriteSheet = EasyExcel.writerSheet(0, "应收挂帐单_")
				.head(FinanceArApAndInvoiceInfoExcelBO.class).build();


		FinanceArApAndInvoiceInfoReqBO financeArApAndInvoiceInfoReqBO = new FinanceArApAndInvoiceInfoReqBO();
		if(Strings.isNotBlank(po.getRequestParam())){
			financeArApAndInvoiceInfoReqBO = JSONObject.parseObject(po.getRequestParam(), FinanceArApAndInvoiceInfoReqBO.class);
		}

		RspPage<FinanceArApAndInvoiceInfoBO> financeArApAndInvoiceInfoBORspPage =  financeArApAndInvoiceInfoService.queryFinanceArApAndInvoiceInfoList(financeArApAndInvoiceInfoReqBO);
		List<FinanceArApAndInvoiceInfoBO> list = financeArApAndInvoiceInfoBORspPage.getRows();

		List<FinanceArApAndInvoiceInfoExcelBO> rangeSheetListBOS = new ArrayList<>(list.size());
		for(int index = 0; index < list.size(); index++){
			FinanceArApAndInvoiceInfoExcelBO excelBO = new FinanceArApAndInvoiceInfoExcelBO();
			BeanUtils.copyProperties(list.get(index), excelBO);
			excelBO.setNo(index+1);
			rangeSheetListBOS.add(excelBO);
		}
		excelWriter.write(rangeSheetListBOS, orderWriteSheet);

		excelWriter.finish();
		String fileName = this.getFileName(null);

//		FileInfoRspBO fileRspBO = excelExportHelper.uploadFileToOss(new ByteArrayInputStream(out.toByteArray()), fileName, reqInfo);

		String url ="";
		Long uploadId = 10L;

		return new CommonExportResultBO(ExportResultStatus.SUCCESS.getVal(), url, uploadId);
	}

}